package components.utils
{
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class URLRequestUtil
	{
		private static var _cacheID:uint=new Date().getTime();

		public static var noCache:Boolean=true;

		public function URLRequestUtil()
		{
		}

		/**
		 *获取一个不会缓存的加载请求
		 * @param url
		 * @return
		 *
		 */
		public static function getNoCacheURLRequest(url:String):URLRequest
		{
			var urlRequest:URLRequest=new URLRequest(url);

			if (noCache)
				setRequestURL(urlRequest, url, "cacheID=" + _cacheID);

			return urlRequest;
		}

		/**
		 *设置加载参数
		 * @param request
		 * @param url
		 * @param extraParams
		 *
		 */
		public static function setRequestURL(request:URLRequest, url:String, extraParams:String=""):void
		{
			var a:Array=url.split("?");

			var s:String=a[0];
			var parsedURL:String="";
			for (var i:int=0; i < s.length; i++)
			{
				parsedURL+=s.charAt(i);
			}

			request.url=parsedURL;
			if (a.length >= 2)
			{
				extraParams+=(extraParams == "") ? a[1] : "&" + a[1];
			}
			if (extraParams != "")
			{
				var data:URLVariables=(request.data == null) ? new URLVariables() : request.data as URLVariables;
				a=extraParams.split("&");
				i=a.length;
				var pair:Array;
				while (--i > -1)
				{
					pair=a[i].split("=");
					data[pair.shift()]=pair.join("=");
				}
				request.data=data;
			}
		}
	}
}